package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.apache.struts2.json.annotations.JSON;

import sjtu.ist.warehouse4ws.model.Company;
import sjtu.ist.warehouse4ws.model.RegistApplication;
import sjtu.ist.warehouse4ws.model.ShiroRole;
import sjtu.ist.warehouse4ws.model.ShiroUser;
import sjtu.ist.warehouse4ws.service.CompanyService;
import sjtu.ist.warehouse4ws.service.CreateTableService;
import sjtu.ist.warehouse4ws.service.RegistApplicationService;
import sjtu.ist.warehouse4ws.service.ShiroRoleService;
import sjtu.ist.warehouse4ws.service.ShiroUserService;

import com.opensymphony.xwork2.ActionSupport;

public class RegistAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private ShiroUserService shiroUserService;
	private ShiroRoleService shiroRoleService;
	private CompanyService companyService;
	private CreateTableService createTableService;
	private RegistApplicationService registApplicationService;

	private boolean success;
	private String message;
	private String userName;
	private String pw1;
	private String pw2;
	private String companyName;
	private String id;
	
	private List<RegistApplication> registApplicationList=new ArrayList<RegistApplication>();
	
	public void setShiroUserService(ShiroUserService shiroUserService) {
		this.shiroUserService = shiroUserService;
	}
	
	public void setCompanyService(CompanyService companyService){
		this.companyService = companyService;
	}
	
//	@JSON(name = "isUserNameRegisted")
//	public boolean isUserNameRegisted() {
//		return userService.isUserNameRegisted(userName, pw1);
//	}
//
//	@JSON(name = "isCompanyNameRegisted")
//	public boolean isCompanyRegisted() {
//		return userService.isCompanyNameRegisted(userName, pw1,
//				companyName);
//	}
//	

	public Integer regist(RegistApplication ra) {
		ShiroUser user = new ShiroUser();
		Company company = new Company();
//		System.out.println(companyName);
//		System.out.println(pw1);
//		System.out.println(userName);
		company.setName(ra.getCompany());
		Integer cid=companyService.save(company);
		
		//privilegeService.addPrivilegeForCompany(companyId);		
		user.setCompanyId(cid);
		user.setUsername(ra.getName());
		user.setPassword(ra.getPassword());
		HashSet<ShiroRole> srs=new HashSet<ShiroRole>(shiroRoleService.findByName("admin"));
		user.setShiroRoles(srs);
		shiroUserService.saveShiroUser(user);

		return cid;
	}

	public String execute() throws Exception {
		String str1="用户名已经被注册，请重新选择用户名";
		String str2="公司名已经被注册，请联系管理员";
		String str3="成功提交用户注册申请，请等待管理员审批。";
		success=true;
		
		ShiroUser su=shiroUserService.findUser(getUserName());
		if(su!=null){
			message = str1;
			return SUCCESS;
		}
		List<Company> com=companyService.findByName(getCompanyName());
		if(com.size()>0){
			message = str2;
			return SUCCESS;
		}
		
		RegistApplication ra=new RegistApplication(userName,pw1,companyName);
		System.out.println(ra.getName()+' '+ra.getPassword()+' '+ra.getCompany());
		registApplicationService.save(ra);
		
		//Integer cid=regist();
		//createTableService.createTable(cid.toString());
		
		message = str3;
		return SUCCESS;
	}
	
	public String showRegistApplicationList() throws Exception {
		registApplicationList=registApplicationService.getAll();
		success=true;
		return SUCCESS;
	}
	
	public String approveRegist() throws Exception {
		RegistApplication ra=registApplicationService.get(Integer.parseInt(id));;
		Integer cid=regist(ra);
		createTableService.createTable(cid.toString());		
		registApplicationService.delete(Integer.parseInt(id));
		return SUCCESS;
	}

	public String rejectRegist() throws Exception {
		registApplicationService.delete(Integer.parseInt(id));
		return SUCCESS;
	}
	
	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserName() {
		return userName;
	}

	

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	@JSON(name = "success")
	public boolean isSuccess() {
		return success;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	@JSON(name = "message")
	public String getMessage() {
		return message;
	}

	public void setPw1(String pw1) {
		this.pw1 = pw1;
	}

	public String getPw1() {
		return pw1;
	}

	public void setPw2(String pw2) {
		this.pw2 = pw2;
	}

	public String getPw2() {
		return pw2;
	}

	public void setCreateTableService(CreateTableService createTableService) {
		this.createTableService = createTableService;
	}

	public void setShiroRoleService(ShiroRoleService shiroRoleService) {
		this.shiroRoleService = shiroRoleService;
	}

	public ShiroRoleService getShiroRoleService() {
		return shiroRoleService;
	}

	public void setRegistApplicationService(RegistApplicationService registApplicationService) {
		this.registApplicationService = registApplicationService;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param registApplicationList the registApplicationList to set
	 */
	public void setRegistApplicationList(List<RegistApplication> registApplicationList) {
		this.registApplicationList = registApplicationList;
	}

	/**
	 * @return the registApplicationList
	 */
	@JSON(name = "registApplicationList")
	public List<RegistApplication> getRegistApplicationList() {
		return registApplicationList;
	}
	
	@JSON(name = "totalRegistApplication")
	public int getTotalRegistApplication(){
		return registApplicationList.size();
	}
}
